package com.ld.security.shop.crawler.modules.wo.api;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.ld.security.common.core.base.vo.DataVo;
import com.ld.security.common.core.base.vo.ListVo;
import com.ld.security.common.core.base.vo.PageVo;
import com.ld.security.common.core.util.JsonUtil;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.core.util.SimpExcelUtil;
import com.ld.security.common.mybatis.base.po.IdsPo;
import com.ld.security.common.tomcat.base.api.*;

import com.ld.security.common.tomcat.util.RespUtil;
import com.ld.security.shop.crawler.modules.wo.entity.WoData;

import com.ld.security.shop.crawler.modules.wo.service.IWoDataService;
import com.ld.security.shop.crawler.modules.wo.service.IWoGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags="资源解析Api管理")
@RequestMapping("/api/woData")
public class  WoDataApi extends  SuperApi<WoData> {

    @Autowired
    IWoDataService iWoDataService;


    public void exportDataToJson(List<WoData> woDataList){
        List list =  woDataList.stream().map(val -> {
            return JsonUtil.toObject(val.getContent(),Map.class);
        }).collect(Collectors.toList());
        ListVo vo =      ListVo.of(list);

        RespUtil.downloadFile("data.json",JsonUtil.toJson(vo).getBytes());
        //RespUtil.writeJson(dataVo);
    }


    public  void exportDataToExcel(List<WoData> woDataList){
        List list =  woDataList.stream().map(val -> {
            return JsonUtil.toObject(val.getContent(),Map.class);
        }).collect(Collectors.toList());
        byte[] data =  SimpExcelUtil.getMapWriter(list);
        RespUtil.downloadFile("data.xls",data);
    }

    @ApiOperation("导出数据为json")
    @RequestMapping(value = "/exportDataToJson",method = RequestMethod.GET)
    public void exportDataToJson(WoData q){
        List<WoData> woDataList =  iWoDataService.all(q);
        this.exportDataToJson(woDataList);
    }

    @ApiOperation("导出数据为excel")
    @RequestMapping(value = "/exportDataToExcel",method = RequestMethod.GET)
    public  void exportDataToExcel(WoData q){
        List<WoData> woDataList =  iWoDataService.all(q);
        exportDataToExcel(woDataList);

    }



}
